package gui.Teacher;

import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import control.RemoveQuestionControl;

import BaseClasses.Question;
import System.MainControl;
import System.SysMode;
import System.SystemObject;
import javax.swing.JComboBox;

public class RemoveQuestionGui extends JPanel implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3161653303457841288L;
	public JButton btnBack_1 = new JButton("Back");
	public JButton btnRemQuest = new JButton("Remove Question");
	private Question q;
	private RemoveQuestionControl control;
	private JComboBox comboBox;
	private JTextField textField;
	private JButton btnLoad;
	List<Question> questionList;
	
	public RemoveQuestionGui() {
	    super();
	    setBackground(SystemColor.activeCaption);
		control = new RemoveQuestionControl();
		initialize();
}


private void initialize() {
	
	this.setSize(500, 500);
	this.setLayout(null);
	setBorder(new TitledBorder(null, "Remove Question", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	
	JLabel lblSetQuestId = new JLabel(" Question ID");
	lblSetQuestId.setHorizontalAlignment(SwingConstants.CENTER);
	lblSetQuestId.setFont(new Font("Tahoma", Font.BOLD, 14));
	lblSetQuestId.setBounds(46, 120, 105, 14);
	add(lblSetQuestId);
	btnRemQuest.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	btnRemQuest.addActionListener(this);
	btnRemQuest.setBounds(65, 312, 164, 23);
	add(btnRemQuest);
	
	btnBack_1.setFont(new Font("Microsoft Tai Le", Font.BOLD, 14));
	btnBack_1.setBounds(278, 312, 89, 23);
	btnBack_1.addActionListener(this);
	add(btnBack_1);
	
	comboBox = new JComboBox();
	comboBox.setBounds(178, 119, 86, 20);
	SystemObject sys = new SystemObject(null, SysMode.getQuestions);
	control.sendToServer(sys);
	
	questionList = null;
	SystemObject retObj = control.GetSysObj();
	if (retObj.getMode() == SysMode.OK)
		questionList = (List<Question>)retObj.getObj();
	
	if (questionList != null)
	{
		for (int i=0;i<questionList.size();i++)
		{
			comboBox.addItem(questionList.get(i).getID());
		}
	}
	add(comboBox);
	
	textField = new JTextField();
	textField.setEditable(false);
	textField.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	textField.setBounds(89, 163, 290, 89);
	add(textField);
	textField.setColumns(10);
	
	btnLoad = new JButton("Load");
	btnLoad.addActionListener(this);
	btnLoad.setFont(new Font("Tahoma", Font.BOLD, 13));
	btnLoad.setBounds(290, 118, 89, 23);
	add(btnLoad);
}
@Override			
public void actionPerformed(ActionEvent e) 
{
	if(e.getSource() == btnBack_1)
	{
		MainControl.setGUI(MainControl.currentUser.getRole(), this);
	}
	
	if(e.getSource()==btnRemQuest)
	{
		String id=(String)comboBox.getSelectedItem();
		q = new Question(id);
		
		SystemObject sys = new SystemObject(q, SysMode.RemoveQuestion);
		if (control.sendToServer(sys) == true)
		{
			JOptionPane.showMessageDialog(null,"Delete Success","System Message",JOptionPane.INFORMATION_MESSAGE);
			MainControl.setGUI(MainControl.currentUser.getRole(), this); 	
		}
	}
	if(e.getSource() == btnLoad)
		{
		int Qid=comboBox.getSelectedIndex();
		textField.setText(questionList.get(Qid).getText());		
		}
	}
}